using System;
using System.Collections.Generic;
using System.Text;
using Common.Shared.Design;
using System.ComponentModel;

namespace Common.Shared.Xml.Wddx {
	[WddxRoot ( "dateTime" )]
	public class WddxDateTime : WddxPrimitiveDataTypeObject {
		public const string DATEFORMAT = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK";
		private DateTime _value;

		public WddxDateTime ( ) : this(DateTime.Now){
		}

		public WddxDateTime ( DateTime value ) {
			this.Value = value;
		}

		public new DateTime Value {
			get { return _value; }
			set { _value = value; }
		}

		[WddxText]
		[EditorBrowsable ( EditorBrowsableState.Never ), Browsable ( false )]
		public string ValueString {
			get { return this.Value.ToString ( DATEFORMAT ); }
			set {
				DateTime dt = DateTime.Now;
				if ( !DateTime.TryParse ( value, out dt ) ) {
					throw new WddxException ( string.Format ( "Unable to parse '{0}' as a valid date/time.", value ) );
				} else {
					this.Value = dt;
				}
			}
		}
	}
}
